﻿
using UnityEngine;
namespace FrameWorkSong
{
    public static class ColorExtention
    {
        /// <summary>
        /// 十六进制颜色码转换成RGB颜色值
        /// </summary>
        /// <param name="color">本颜色</param>
        /// <param name="colorStr"> 十六进制颜色码</param>
        /// <param name="Alpha">透明度</param>
        /// <returns></returns>
        public static Color ToRGB(this Color color,string colorStr, float Alpha)
        {
            Color mColor = new Color();
            mColor.ToRGB(colorStr);
            color = new Color(mColor.a, mColor.g, mColor.b, Alpha);
            return color;
        }
        /// <summary>
        /// 十六进制颜色码转换成RGB颜色值
        /// </summary>
        /// <param name="color">本颜色</param>
        /// <param name="colorStr">十六进制颜色码</param>
        /// <returns></returns>
        public static Color ToRGB(this Color color, string colorStr)
        {
            char[] strCharArr = colorStr.ToCharArray();
            int i = 0;
            i = strCharArr[0].ToString() == "#" ? 1 : 0;

            int r1 = MathUtil.SixteenNormalize(strCharArr[i + 0].ToString());
            int r2 = MathUtil.SixteenNormalize(strCharArr[i + 1].ToString());
            int g1 = MathUtil.SixteenNormalize(strCharArr[i + 2].ToString());
            int g2 = MathUtil.SixteenNormalize(strCharArr[i + 3].ToString());
            int b1 = MathUtil.SixteenNormalize(strCharArr[i + 4].ToString());
            int b2 = MathUtil.SixteenNormalize(strCharArr[i + 5].ToString());

            float R = ((r1 * 16) + r2) / 256.000f;
            float G = ((g1 * 16) + g2) / 256.000f;
            float B = ((b1 * 16) + b2) / 256.000f;

            color = new Color(R, G, B,1);
            return color;
        }
    }
}

